package com.example.daxiguaforharmony.game.core.renderers;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.RectFloat;
import ohos.media.image.common.Size;

public class Scale9Sprite extends Sprite {
    protected PixelMapHolder _pixelMapHolder = null;
    private RectFloat _rectSrc = new RectFloat();
    private RectFloat _rectDst = new RectFloat();

    public Scale9Sprite(PixelMapHolder pixelMapHolder) {
        super(pixelMapHolder);
    }

    @Override
    public void replacePixelMapHolder(PixelMapHolder pixelMapHolder) {
        assert pixelMapHolder != null;
        this._pixelMapHolder = pixelMapHolder;
        Size size = pixelMapHolder.getPixelMap().getImageInfo().size;
        this.width = size.width;
        this.height = size.height;
    }

    @Override
    public void render(Canvas canvas, Paint paint) {
        super.render(canvas, paint);
        if (this._pixelMapHolder != null) {
            this._rectSrc.left = 0;
            this._rectSrc.top = 0;
            this._rectSrc.right = this.rawWidth;
            this._rectSrc.bottom = this.rawHeight;
            float px = this.x - this.width * this.scaleX / 2f;
            float py = this.y - this.height * this.scaleY / 2f;
            this._rectDst.left = px;
            this._rectDst.top = py;
            this._rectDst.right = px + this.width * this.scaleX;
            this._rectDst.bottom = py + this.height * this.scaleY;
            canvas.save();
            canvas.rotate(this.rotation, this.x, this.y);
            paint.setAlpha(this.alpha);
            canvas.drawPixelMapHolderRect(this._pixelMapHolder, this._rectSrc, this._rectDst, paint);
            canvas.restore();
        }
    }

    @Override
    public void destory() {
        this._pixelMapHolder = null;
        this._rectSrc = null;
        this._rectDst = null;
    }
}
